'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function() {
        this.initCreate();
        this.initDelete();
        this.initLink();
        this.save();
    },
    
    
    initCreate: function(){
        var that = this;
        
        $(document).on("shown.bs.modal","#create, #edit", function(e) {
            var $form = $(e.currentTarget).find("form");
            
              $form.bt_validate();
              
              $form.shiftform({
                beforeSubmit : function(el) {
                  $('form').trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              });
        })
             
    },
 
      initDelete: function(){
          $(document).on("shown.bs.modal","#rest, #delete", function(e) {
               var $form = $(e.currentTarget).find("form");
               $form.shiftform();
          })
               
      },
      
      initLink: function(){
          $(document).on("shown.bs.modal","#link", function(e) {
          })
               
      },
      
      save: function(){
          
          $(document).on("click",".btn-save-all", function(e) {
              $.ajax({
                  type: 'PATCH',
                  url: window.__CTX__.root + '/grouping-templates/1/weekly-templates/1',
                  data: '1',
                  cache:false
                }).done(function(data) {
                    
                    var toastHtml = "<div class='toast-section'>"+
                                       "<p class='toast-text'>保存成功！</p>"+
                                     "</div>"
                    $("body").append(toastHtml);
                    setTimeout(' $(".toast-section").remove();',1500)
                }).fail(function() {
                    var toastHtml = "<div class='toast-section'>"+
                                       "<p class='toast-text'>保存失败！</p>"+
                                     "</div>"
                    $("body").append(toastHtml)
                    setTimeout(' $(".toast-section").remove();',1500)
                })
         })
      },
        
      
   }
})
